package cn.wp.ewallet.wallet.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.wp.common.core.util.BeanUtil;
import cn.wp.ewallet.common.model.dto.MoneyTypeInfoDto;
import cn.wp.ewallet.common.service.MoneyTypeInfoService;
import cn.wp.ewallet.wallet.dao.StatInfoDao;
import cn.wp.ewallet.wallet.extension.constant.EwalletWalletConstant;
import cn.wp.ewallet.wallet.extension.util.EwalletWalletUtil;
import cn.wp.ewallet.wallet.model.dto.ServiceChargeStatDto;
import cn.wp.ewallet.wallet.model.dto.StatInfoDto;
import cn.wp.ewallet.wallet.model.entity.ServiceChargeStatDoo;
import cn.wp.ewallet.wallet.model.entity.StatInfoDo;
import cn.wp.ewallet.wallet.service.ServiceChargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yangjin on 2020/2/5
 */
@Service
public class ServiceChargeServiceImpl implements ServiceChargeService {

    @Autowired
    private StatInfoDao statInfoDao;

    @Autowired
    private MoneyTypeInfoService moneyTypeInfoService;

    @Override
    public Map<String, StatInfoDto> getStatInfo() {

        Map<String, StatInfoDto> statInfoDtoMap = new HashMap<>();
        //查询SHQC数据精度
        MoneyTypeInfoDto shqcMoney = moneyTypeInfoService.getMoneyTypeInfoByMoneyTypeId(EwalletWalletConstant.MONEY_TYPE_SHQC);
        BigDecimal shqcUnit = BigDecimal.valueOf(Long.valueOf(shqcMoney.getMoneyTypeUnit()));
        //查询USDT数据精度
        MoneyTypeInfoDto usdtMoney = moneyTypeInfoService.getMoneyTypeInfoByMoneyTypeId(EwalletWalletConstant.MONEY_TYPE_USDT);
        BigDecimal usdtUnit = BigDecimal.valueOf(Long.valueOf(usdtMoney.getMoneyTypeUnit()));

        /**
         *  1000-SHQC转账手续费
         */
        StatInfoDo transStatInfoDo = statInfoDao.selectstatInfoByStatType(EwalletWalletConstant.STAT_TYPE_SHQC_TRANS_ACCOUNT_SERVICE_CHARGE, LocalDate.now().minusDays(1).toString());
        if(null != transStatInfoDo) {
            StatInfoDto transStatInfoDto = BeanUtil.copyProperties(transStatInfoDo, StatInfoDto.class);
            transStatInfoDto.setTotalCurrency(EwalletWalletUtil.toUpperWei(transStatInfoDto.getTotalCurrency(),shqcUnit));
            transStatInfoDto.setYesCurrency(EwalletWalletUtil.toUpperWei(transStatInfoDto.getYesCurrency(),shqcUnit));
            statInfoDtoMap.put("transStatInfoDto", transStatInfoDto);
        }


        /**
         *  1100-USDT提币手续费
         */
        StatInfoDo usdtMentionStatInfoDo = statInfoDao.selectstatInfoByStatType(EwalletWalletConstant.STAT_TYPE_USDT_MENTION_SERVICE_CHARGE, LocalDate.now().minusDays(1).toString());
        if(null != usdtMentionStatInfoDo) {
            StatInfoDto usdtMentionStatInfoDto = BeanUtil.copyProperties(usdtMentionStatInfoDo, StatInfoDto.class);
            usdtMentionStatInfoDto.setTotalCurrency(EwalletWalletUtil.toUpperWei(usdtMentionStatInfoDto.getTotalCurrency(),usdtUnit));
            usdtMentionStatInfoDto.setYesCurrency(EwalletWalletUtil.toUpperWei(usdtMentionStatInfoDto.getYesCurrency(),usdtUnit));
            statInfoDtoMap.put("usdtMentionStatInfoDto", usdtMentionStatInfoDto);
        }

        /**
         *  1200-SHQC提币手续费
         */
        StatInfoDo shqcMentionStatInfoDo = statInfoDao.selectstatInfoByStatType(EwalletWalletConstant.STAT_TYPE_SHQC_MENTION_SERVICE_CHARGE, LocalDate.now().minusDays(1).toString());
        if(null != shqcMentionStatInfoDo) {
            StatInfoDto shqcMentionStatInfoDto = BeanUtil.copyProperties(shqcMentionStatInfoDo, StatInfoDto.class);
            shqcMentionStatInfoDto.setTotalCurrency(EwalletWalletUtil.toUpperWei(shqcMentionStatInfoDto.getTotalCurrency(), shqcUnit));
            shqcMentionStatInfoDto.setYesCurrency(EwalletWalletUtil.toUpperWei(shqcMentionStatInfoDto.getYesCurrency(), shqcUnit));
            statInfoDtoMap.put("shqcMentionStatInfoDto", shqcMentionStatInfoDto);
        }
        return statInfoDtoMap;
    }

    @Override
    public List<ServiceChargeStatDto> getServiceChargeStatInfo(String year, String month) {
        List<ServiceChargeStatDto> serviceChargeStatDtos = new ArrayList<>();

        //查询SHQC数据精度
        MoneyTypeInfoDto shqcMoney = moneyTypeInfoService.getMoneyTypeInfoByMoneyTypeId(EwalletWalletConstant.MONEY_TYPE_SHQC);
        BigDecimal shqcUnit = BigDecimal.valueOf(Long.valueOf(shqcMoney.getMoneyTypeUnit()));
        //查询USDT数据精度
        MoneyTypeInfoDto usdtMoney = moneyTypeInfoService.getMoneyTypeInfoByMoneyTypeId(EwalletWalletConstant.MONEY_TYPE_USDT);
        BigDecimal usdtUnit = BigDecimal.valueOf(Long.valueOf(usdtMoney.getMoneyTypeUnit()));

        List<ServiceChargeStatDoo> serviceChargeStatDoos = statInfoDao.selectServiceChargeStatInfo(year, month);



        if(CollectionUtil.isNotEmpty(serviceChargeStatDoos)) {
            for (ServiceChargeStatDoo serviceChargeStatDoo : serviceChargeStatDoos) {
                ServiceChargeStatDto serviceChargeStatDto = BeanUtil.copyProperties(serviceChargeStatDoo,ServiceChargeStatDto.class);
                serviceChargeStatDto.setTransAccountAmount(EwalletWalletUtil.toUpperWei(serviceChargeStatDoo.getTransAccountAmount(), shqcUnit));
                serviceChargeStatDto.setUsdtMentionAmount(EwalletWalletUtil.toUpperWei(serviceChargeStatDoo.getUsdtMentionAmount(), usdtUnit));
                serviceChargeStatDto.setShqcMentionAmount(EwalletWalletUtil.toUpperWei(serviceChargeStatDoo.getShqcMentionAmount(), shqcUnit));
                serviceChargeStatDtos.add(serviceChargeStatDto);
            }
        }
        return serviceChargeStatDtos;
    }
}
